eth_ecrecover

(alias) function eth_ecrecover(r: byte_array, s: byte_array, rec_id: integer, data_hash: byte_array): byte_array

Compute an Ethererum public key from a signature and a hash.

Similar to Solidity's ecrecover(), though differs in that:

  • This function takes rec_id, rather than v, where rec_id == v - 27.

  • The parameter order is different.

  • This function returns a 64-byte public key, not a 20-byte address. An address can be obtained by taking the last 20 bytes of the keccak256() digest of the returned public key, e.g.:

 val address: byte_array = keccak256(eth_ecrecover(...)).sub(44);

The signature (consisting of the r, s and rec_id components) will typically be obtained with a procedure equivalent to eth_sign(data_hash, privkey), where privkey and pubkey form a keypair (pubkey being returned form this method).

The signature component rec_id is an adjusted recovery identifier, equivalent to Ethereum's recovery identifier (usually denoted as v) minus 27, i.e. rec_id == v - 27.

The given 32-byte array data_hash is typically a cryptographic hash obtained from a larger data structure using a hashing function such as hash(), sha256() or keccak256().

Example

The following is a Node.js script which uses ecrecover() (the equivalent to crypto.eth_ecrecover()) from the Ethereum Web3 library:

const Web3 = require('web3');
const web3 = new Web3();

var r = '0xcf722a47bcf1da61967ccc6405e31db4d37bce153255a6937e5cceb222caead0';
var s = '0xcf722a47bcf1da61967ccc6405e31db4d37bce153255a6937e5cceb222caead0';
var h = '0x53d7b11e61a8059aa4bc3248d24b2936436c9796dfe7f18e414c181004f79427';
var v = '0x1c';

var address = web3.eth.accounts.recover({'r':r,'s':s,'messageHash':h,'v':v});
console.log(address); // prints 0x5b0c087542D5C1E66Df0041e179c4201675B1614

An equivalent script in Rell is as follows:

val r = x'cf722a47bcf1da61967ccc6405e31db4d37bce153255a6937e5cceb222caead0';
val s = x'cf722a47bcf1da61967ccc6405e31db4d37bce153255a6937e5cceb222caead0';
val h = x'53d7b11e61a8059aa4bc3248d24b2936436c9796dfe7f18e414c181004f79427';
val v = 0x1c;

val pubkey = eth_ecrecover(r, s, v - 27, h);
val address = keccak256(pubkey).sub(12);
print(address); // prints 0x5b0c087542d5c1e66df0041e179c4201675b1614

Note that in the Rell script, v is an integer, while in the Node.js script it is an 0x-prefixed hexadecimal string.

Alias

Return

a 64-byte public key

Since

0.10.6

Parameters

r

the first component of the Ethereum signature

s

the second component of the Ethereum signature

rec_id

the recovery identifier, normally 0 or 1

data_hash

the original (unsigned) 32-byte array